package com.ruoyi.merchant.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.merchant.domain.vo.ActivityVO;
import com.ruoyi.merchant.task.ActivityTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.merchant.mapper.ActivityMapper;
import com.ruoyi.merchant.domain.Activity;
import com.ruoyi.merchant.service.IActivityService;

/**
 * 活动Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
@Slf4j
public class ActivityServiceImpl implements IActivityService 
{
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询活动
     * 
     * @param aId 活动主键
     * @return 活动
     */
    @Override
    public Activity selectActivityByAId(Long aId)
    {
        return activityMapper.selectActivityByAId(aId);
    }

    /**
     * 查询活动列表
     * 
     * @param activityVO 活动
     * @return 活动
     */
    @Override
    public List<ActivityVO> selectActivityList(ActivityVO activityVO)
    {
        return activityMapper.selectActivityList(activityVO);
    }

    /**
     * 查询 审核通过1 未发布0 未删除0 的活动列表
     *
     * @param activityVO
     * @return
     */
    @Override
    public List<ActivityVO> selectActivityPassedAndUnpublishedList(ActivityVO activityVO) {
        return null;
    }

    /**
     * 查询 审核通过1 已发布1 未删除0 的活动列表
     *
     * @param activityVO
     * @return
     */
    @Override
    public List<ActivityVO> selectActivityPassedAndPublishedList(ActivityVO activityVO) {
        return null;
    }

    /**
     * 查询 审核通过1 已下架2 未删除0 的活动列表
     *
     * @param activityVO
     * @return
     */
    @Override
    public List<ActivityVO> selectActivityPassedAndDownList(ActivityVO activityVO) {
        return null;
    }

    /**
     * 查询 审核中0 未发布0 未删除0 的活动列表
     *
     * @param activityVO
     * @return
     */
    @Override
    public List<ActivityVO> selectActivityPassingAndUnpublishedList(ActivityVO activityVO) {
        return null;
    }

    /**
     * 查询 审核不通过2 未发布0 未删除0 的活动列表
     *
     * @param activityVO
     * @return
     */
    @Override
    public List<ActivityVO> selectActivityUnpassedAndUnpublishedList(ActivityVO activityVO) {
        return null;
    }

    /**
     * 新增活动
     * 
     * @param activity 活动
     * @return 结果
     */
    @Override
    public int insertActivity(Activity activity)
    {
        return activityMapper.insertActivity(activity);
    }

    /**
     * 修改活动
     * 
     * @param activity 活动
     * @return 结果
     */
    @Override
    public int updateActivity(Activity activity)
    {
        return activityMapper.updateActivity(activity);
    }

    /**
     * 批量删除活动
     * 
     * @param aIds 需要删除的活动主键
     * @return 结果
     */
    @Override
    public int deleteActivityByAIds(Long[] aIds)
    {
        return activityMapper.deleteActivityByAIds(aIds);
    }

    /**
     * 删除活动信息
     * 
     * @param aId 活动主键
     * @return 结果
     */
    @Override
    public int deleteActivityByAId(Long aId)
    {
        return activityMapper.deleteActivityByAId(aId);
    }

    /**
     * 发布活动
     *
     * @param activityId
     */
    public void publishActivity(Long activityId) {
        ActivityVO activityVO = activityMapper.selectActivityByAId(activityId);
        activityVO.setStatus(1L);
        // 更新发布状态
        activityMapper.updateActivity(activityVO);
        log.info("活动已发布: {}", activityId);
    }

    /**
     * 下架活动
     *
     * @param activityId
     */
    public void downActivity(Long activityId) {
        ActivityVO activityVO = activityMapper.selectActivityByAId(activityId);
        activityVO.setStatus(2L);
        // 标记删除
        activityMapper.updateActivity(activityVO);
        log.info("活动已结束: {}", activityId);
    }

    /**
     * 修改活动发布状态并添加任务
     *
     * @param activity
     * @return
     */
    @Override
    public Boolean updateAndStoreActivity(Activity activity) {

        // 1. 添加开始任务
        ActivityTask startTask = new ActivityTask();
        startTask.setActivityId(activity.getaId());
        startTask.setType("publish");
        redisService.zAdd("activity:tasks",
                JSON.toJSONString(startTask),
                activity.getPublishTime().getTime() / 1000.0
        );

        // 2. 添加结束任务
        ActivityTask endTask = new ActivityTask();
        endTask.setActivityId(activity.getaId());
        endTask.setType("down");
        redisService.zAdd("activity:tasks",
                JSON.toJSONString(endTask),
                activity.getDownTime().getTime() / 1000.0
        );
        return true;
    }

}
